<?php

/**
 * Description of User_model
 *
 * @author Administrator
 */
class User_model extends CI_Model {

  //put your code here
  protected $table = '';
  protected $class = '';
  protected $base_lib = '';

  public function __construct() {
    parent::__construct();
    $this->class = get_class($this);
    $this->table = constant('DB_TABLE_' . strtoupper(substr($this->class, 0, strpos($this->class, '_model'))));
    $this->base_lib = new Base_lib($this->table);
  }

  public function create_obj($params) {
    $id = empty($params['id']) ? null : $params['id'];
    $obj = array('id' => $id);
    if ($id) {
      $obj = $this->db->get_where($this->table, array('id' => $id), 1, 0)->result_array();
      if (!empty($obj)) {
        $obj = $obj[0];
      }
    }
    foreach ($this->db->list_fields($this->table) as $field) {
      if ($field != 'id' && isset($params[$field])) {
        $obj[$field] = $params[$field];
      }
    }
    if (isset($params['confirm_pass'])) {
      $obj['confirm_pass'] = $params['confirm_pass'];
    }
    return $obj;
  }

  public function validate($obj) {
    $errors = array();
    if (empty($obj['username']))
      $errors['username'] = $this->lang->line('error.field.require', 'username');
    else if (strlen($obj['username']) < 3)
      $errors['username'] = $this->lang->line('error.field.min', array('username', 6));
//    else if (!validate_alphanumeric_underscore($obj['username']))
//      $errors['username'] = $this->lang->line('error.field.allow.character', $obj['username']);
    else if (!$this->base_lib->check_exist($obj['username'], 'username', $obj['id']))
      $errors['username'] = $this->lang->line('error.field.exist', array('Username', $obj['username']));
    if (empty($obj['id'])) {
      if (empty($obj['password']))
        $errors['password'] = $this->lang->line('error.field.require', 'password');
      else if (strlen($obj['password']) < 6)
        $errors['password'] = $this->lang->line('error.field.min', array('password', 6));
      if (empty($obj['confirm_pass']))
        $errors['confirm_pass'] = $this->lang->line('error.field.require', 'Confirm password');
    } else if (strlen($obj['password']) < 6 && strlen($obj['password']) > 0)
      $errors['password'] = $this->lang->line('error.field.min', array('password', 6));
    if (!empty($obj['password']) && !empty($obj['confirm_pass']) && $obj['password'] != $obj['confirm_pass'])
      $errors['confirm_pass'] = $this->lang->line('error.field.require.confirm', 'password');
    return $errors;
  }

  protected function set_condition($where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array(), $group_by = array(), $is_distinct = false) {
    $flag = false;
    foreach (get_defined_vars() as $name => $value) {
      if (!empty($value) && is_array($value)) {
        foreach ($value as $field => $condition) {
          $this->db->$name($field, $condition);
          $flag = true;
        }
      }
    }
    if (!empty($is_distinct)) {
      $this->db->distinct();
      $flag = true;
    }
    return $flag;
  }

  public function to_string($object_or_id) {
    if (!is_array($object_or_id))
      $object_or_id = $this->get($object_or_id);
    if ($object_or_id) {
      if(empty($object_or_id['display_name'])){
        return $object_or_id['first_name'].' '.$object_or_id['last_name'];
      }
      return $object_or_id['display_name'];
    } else
      return '';
  }

  public function get($where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array()) {
    if (!empty($where)) {
      if (!is_array($where)) {
        $where = array('id' => $where);
      }
    } else
      $where = null;
    $flag = $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, 
            $or_where_not_in, $like, $or_like, $not_like, $or_not_like);
    if (!$flag)
      return null;
    $object = $this->db->get($this->table, 1, 0)->result_array();
    if (!empty($object)) {
      return $object[0];
    } else {
      return null;
    }
  }

  public function get_all($where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array(), $group_by = array(), $is_distinct = false, $order_by = array(), $max = 0, $offset = 0) {
    $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, $or_where_not_in, $like, $or_like, $not_like, $or_not_like, $group_by, $is_distinct);
    if (!empty($order_by)) {
      if (is_array($order_by)) {
        foreach ($order_by as $sort => $order) {
          $this->db->order_by($sort, $order);
        }
      } else {
        $this->db->order_by($order_by, 'asc');
      }
    }
    if (empty($max)) {
      return $this->db->get($this->table)->result_array();
    } else {
      return $this->db->get($this->table, $max, $offset)->result_array();
    }
  }

  public function count($where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array(), $group_by = array(), $is_distinct = false) {
    $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, $or_where_not_in, $like, $or_like, $not_like, $or_not_like, $group_by, $is_distinct);
    return $this->db->count_all_results($this->table);
  }

  public function insert($params) {
    $object = $this->create_obj($params);
    $errors = $this->validate($object);
    if (empty($errors)) {
      unset($object['confirm_pass']);
      $object['created_on'] = time();
      $object['ip_address'] = $_SERVER['REMOTE_ADDR'];
      $object['active_code'] = empty($params['active_code']) ? '' : $params['active_code'];
      $object['first_name'] = empty($params['first_name']) ? '' : $params['first_name'];
      $object['last_name'] = empty($params['last_name']) ? '' : $params['last_name'];
      $object['display_name'] = empty($params['display_name']) ? '' : $params['display_name'];      
      $object['last_login'] = 0;
      $object['is_active'] = empty($params['is_active']) ? 0 : $params['is_active'];      
      $this->db->insert($this->table, $object);
      $id = $this->db->insert_id();
      if (!empty($id)) {
        return $id;
      } else
        return false;
    } else
      return $errors;
  }

  public function update($params, $where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array()) {
    if (!empty($where)) {
      if (!is_array($where)) {
        $where = array('id' => $where);
      }
    } else
      $where = null;
    $object = $this->create_obj($params);
    $errors = $this->validate($object);
    if (empty($errors)) {
      unset($object['confirm_pass']);
      $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, $or_where_not_in, $like, $or_like, $not_like, $or_not_like);
      return $this->db->update($this->table, $object);
    } else
      return $errors;
  }

  public function save($params) {
    if (!empty($params['id'])) {
      return $this->update($params, array('id' => $params['id']));
    } else{
      return $this->insert($params);
    }
  }

  public function delete($where = array(), $or_where = array(), $where_in =array(), $or_where_in = array(), $where_not_in = array(), $or_where_not_in = array(), $like = array(), $or_like = array(), $not_like = array(), $or_not_like = array()) {
    if (!empty($where)) {
      if (!is_array($where)) {
        $where = array('id' => $where);
      }
    } else
      $where = null;
    $this->set_condition($where, $or_where, $where_in, $or_where_in, $where_not_in, $or_where_not_in, $like, $or_like, $not_like, $or_not_like);
    $this->db->delete($this->table);
    return true;
  }

  public function truncate() {
    $this->db->truncate($this->table);
    return true;
  }

}